/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.Color;
import java.awt.Point;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import model.hitoModel;
import model.roadModel;
import type.hitoType;
import type.roadType;
import view.editElementView;

/**
 *
 * @author JorgeLuis
 */
public class editElementController {
    
    private editElementView view = new editElementView(null, true);
    private mainController mainController;
    private String state = "node";

    public editElementController() {
        this.view.controller(this);
    }
    
    public void show(){
        this.view.show();
    }

    public void mainController(mainController aThis) {
        this.mainController = aThis;
    }

    public void setElement(hitoModel hito) {
        
        state = "hito";
        this.addComboxTypes(type.hitoType.values(), hito.getType().toString());
        
        this.view.getComboboxType().setSelectedItem(hito.getType());        
        this.view.getTextFielName().setText(hito.getName());
        this.view.getTextFieldAlias().setText(hito.getAlias());
        this.view.getPanelEdge().setBackground(hito.getColorEdge());
        this.view.getPanelFill().setBackground(hito.getColorFill());
        
        this.view.setId(hito.getId());
        
        this.view.getPanelEdge().setVisible(true);
        this.view.getLabelEdge().setVisible(true);
        
    }
    
    public void setElement(roadModel road) {
        
        state = "road";
        this.addComboxTypes(type.roadType.values(), road.getType().toString());
        
        this.view.getTextFielName().setText(road.getName());
        this.view.getTextFieldAlias().setText(road.getAlias());
        this.view.getPanelFill().setBackground(road.getColor());
        
        this.view.setId(road.getId());
        
        this.view.getPanelEdge().setVisible(false);
        this.view.getLabelEdge().setVisible(false);
    }
    
    private void addComboxTypes(hitoType[] listType, String select){
        JComboBox combobox = this.view.getComboboxType();
        combobox.removeAllItems();
        
        for (int i = 0; i < listType.length; i++) {
            combobox.addItem(listType[i].toString());
            
            if(listType[i].toString() == select){
               combobox.setSelectedIndex(i); 
            }
        }       
    }
    
    private void addComboxTypes(roadType[] listType, String select){
        JComboBox combobox = this.view.getComboboxType();
        combobox.removeAllItems();
        
        for (int i = 0; i < listType.length; i++) {
            combobox.addItem(listType[i].toString());
            
            if(listType[i].toString() == select){
               combobox.setSelectedIndex(i); 
            }
        }       
    }

    public void editElement(int id, String typeElement, String name, String alias, Color colorFill, Color colorEdge, JPanel panelImage) {
        
        if(this.state == "hito"){
            
            hitoModel senHito = new hitoModel(new Point(0,0));
            
            senHito.setId(id);
            senHito.setType(hitoType.valueOf(typeElement));
            senHito.setName(name);
            senHito.setAlias(alias);
            senHito.setColorFill(colorFill);
            senHito.setColorEdge(colorEdge);
            
            this.mainController.editElement(senHito);
        }
        
        else if(this.state == "road"){
            roadModel senRoad = new roadModel();
            
            senRoad.setId(id);
            senRoad.setType(roadType.valueOf(typeElement));
            senRoad.setName(name);
            senRoad.setAlias(alias);
            senRoad.setColor(colorFill);
            
            this.mainController.editElement(senRoad);
        }
        
        
        this.state              = "node";
        
    }

    
    
    
}
